package v1

import (
	"adminHome/models"
	"adminHome/pkg/app"
	"adminHome/pkg/e"
	"adminHome/pkg/util"
	"encoding/json"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"net/http"
)

func AddOutOrder(c *gin.Context) {
	appG := app.Gin{c}
	data := make(map[string]interface{})
	var OrderDetail []map[string]interface{}

	StorageId := com.StrTo(c.PostForm("storage_id")).MustInt()
	Type := com.StrTo(c.PostForm("type")).MustInt()
	LinkmanTel := c.PostForm("linkman_tel")
	Linkman := c.PostForm("linkman")
	Address := c.PostForm("address")
	TotalNum := com.StrTo(c.PostForm("num")).MustInt()
	TotalPrice := com.StrTo(c.PostForm("price")).MustFloat64()
	Remark := c.PostForm("remark")
	detail := c.PostForm("order_detail")
	json.Unmarshal([]byte(detail), &OrderDetail)

	valid := validation.Validation{}
	valid.Required(StorageId, "storage_id").Message("仓库为必填")
	valid.Required(LinkmanTel, "linkman_tel").Message("联系人电话为必填")
	valid.Required(Linkman, "linkman").Message("联系人为必填")
	valid.Required(Address, "address").Message("地址为必填")
	valid.Required(OrderDetail, "order_detail").Message("产品信息不能为空")

	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	Storage, _ := models.GetStorageById(StorageId)

	data["storage_id"] = StorageId
	data["storage_name"] = Storage.Name
	data["type"] = Type
	data["linkman"] = Linkman
	data["linkman_tel"] = LinkmanTel
	data["address"] = Address
	data["remark"] = Remark
	data["code"] = util.GenerateCode()
	data["total_num"] = TotalNum
	data["total_price"] = TotalPrice

	main_id := models.AddOutOrder(data)

	for _, v := range OrderDetail {
		v["num"] = com.StrTo(v["num"].(string)).MustInt()
		v["product_id"] = int(v["product_id"].(float64))
		models.AddOutOrderDetail(v, main_id)
	}
	code = e.SUCCESS
	appG.Response(http.StatusOK, code, main_id)
}

func EditOutOrder(c *gin.Context) {
	appG := app.Gin{c}
	data := make(map[string]interface{})

	id := com.StrTo(c.PostForm("id")).MustInt()
	totalPrice := com.StrTo(c.PostForm("total_price")).MustFloat64()
	totalNum := com.StrTo(c.PostForm("total_num")).MustInt()
	remark := c.PostForm("remark")

	valid := validation.Validation{}
	valid.Required(id, "id").Message("id为必填")
	valid.Required(totalPrice, "total_price").Message("总价为必填")
	valid.Required(totalNum, "total_num").Message("总数量为必填")

	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	data["order_status"] = 2 //已审核
	data["remark"] = remark
	data["total_num"] = totalNum
	data["total_price"] = totalPrice

	if res := models.EditOutOrder(id, data); !res {
		code = e.ERROR_UPDATE_OUTORDER
		appG.Response(http.StatusOK, code, nil)
		return
	}

	code = e.SUCCESS
	appG.Response(http.StatusOK, code, nil)
}

func GetOutOrder(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()
	valid := validation.Validation{}
	valid.Required(id, "id").Message("id不能为空")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.ExistOutOrderById(id) {
		code = e.ERROR_NOT_EXIST_OUTORDER
		appG.Response(http.StatusOK, code, nil)
		return
	}

	outOrder, _ := models.GetOUtOrder(id)
	code = e.SUCCESS
	appG.Response(http.StatusOK, code, outOrder)
}

func GetOutOrderList(c *gin.Context) {
	appG := app.Gin{c}

	code := c.Query("code")
	linkman := c.Query("linkman")
	linkmanTel := c.Query("linkman_tel")

	data := make(map[string]interface{})
	response := make(map[string]interface{})
	if code != "" {
		data["code"] = code
	}
	if linkman != "" {
		data["linkman"] = linkman
	}
	if linkmanTel != "" {
		data["linkman_tel"] = linkmanTel
	}

	outorder, _ := models.GetOutOrderList(util.GetPage(c), util.GetPageSize(c), data)
	count, _ := models.GetOutOrderTotal(data)

	response["list"] = outorder
	response["total"] = count
	code_ := e.SUCCESS

	appG.Response(http.StatusOK, code_, response)
}

func DeleteOutOrder(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()

	valid := validation.Validation{}
	valid.Required(id, "id").Message("id不能为空")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.ExistOutOrderById(id) {
		code = e.ERROR_NOT_EXIST_OUTORDER
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.DeleteOutOrderById(id) {
		code = e.ERROR_DELETE_OUTORDER
		appG.Response(http.StatusOK, code, nil)
		return
	}

	code = e.SUCCESS
	appG.Response(http.StatusOK, code, nil)
	return
}
